package za.org.jcicada.comms.impl;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import za.org.jcicada.comms.IConnector;
import za.org.jcicada.comms.impl.inprocess.InProcessAcceptor;
import za.org.jcicada.comms.impl.inprocess.InProcessConnector;
import za.org.jcicada.comms.impl.tcp.TcpAcceptor;
import za.org.jcicada.comms.impl.tcp.TcpConnector;
import za.org.jcicada.comms.impl.udp.UdpAcceptor;
import za.org.jcicada.comms.impl.udp.UdpConnector;

public class ConnectorFactory {
    
    private static Map<String, InProcessAcceptor> inProcessAcceptorMap = new HashMap<String, InProcessAcceptor>();
    
    public static IConnector createConnector(URI acceptorLocationURI) {
        if (acceptorLocationURI.getScheme().equals(TcpAcceptor.TCP)) {
            return new TcpConnector(acceptorLocationURI);
        }
        if (acceptorLocationURI.getScheme().equals(UdpAcceptor.UDP)) {
        	return new UdpConnector(acceptorLocationURI);
        }
//        if (acceptorLocationURI.getScheme().equals(KatcpAcceptor.KATCP)) {
//            return new KatcpConnector(acceptorLocationURI);
//        }
        if (acceptorLocationURI.getScheme().equals(InProcessAcceptor.IN_PROCESS)) {
            return new InProcessConnector(acceptorLocationURI);
        }
        return null;
    }
    
    public static void registerInProcessAcceptor(InProcessAcceptor acceptor) {
        inProcessAcceptorMap.put(acceptor.getBaseLocationURI().getAuthority(), acceptor);
    }
    
    public static InProcessAcceptor getInProcessAcceptor(URI acceptorLocationURI) {
        return inProcessAcceptorMap.get(acceptorLocationURI.getAuthority());
    }

}
